package src.main.kotlin.day91

/**
 * 逆变  协变
 * in  out
 * 入参  返回参数
 *
 * in: 父类泛型的对象可以赋值给子类的泛型对象 用in
 * out: 子类泛型对象可以赋值给父类泛型对象  用out
 *
 * 实现泛型的多态
 */

interface Production<out T>{
    fun product():T
}

interface Consumer<in T>{
    fun consume(item:T)
}

interface ProductionConsumer<T>{
    fun product():T
    fun consume(item:T)
}

open class Food

open class FastFood : Food()

class Burger : FastFood()

//生产者
class FoodStore : Production<Food>{
    override fun product(): Food {
        println("Produce food..")
        return Food()
    }


}

class FastFoodStore : Production<FastFood>{
    override fun product(): FastFood {
        println("Produce fastfood..")
        return FastFood()
    }


}

class BurgerStore : Production<Burger>{
    override fun product(): Burger {
        println("Produce burger..")
        return Burger()
    }


}



//消费者
class Everybody :Consumer<Food>{
    override fun consume(item: Food) {
        println("eat  food")
    }

}

class MordernPeople : Consumer<FastFood>{
    override fun consume(item: FastFood) {
        println("eat fast food")
    }

}

class OtherPeople : Consumer<Burger>{
    override fun consume(item: Burger) {
        println(" other eat burger")
    }

}



fun main() {
    //赋值
    val production : Production<Food> = FoodStore()

    //out: 子类泛型对象可以赋值给父类泛型对象  用out
    val production2 : Production<Food> = FastFoodStore()

    val production3 : Production<Food> = BurgerStore()


    //消费者
    //父类泛型对象可以赋值给子类泛型对象
//    val consumer1 : Consumer<Burger> = Everybody()
    val consumer1 : Consumer<FastFood> = Everybody()
    val consumer2 : Consumer<Burger> = MordernPeople()
    val consumer3 : Consumer<Burger> = OtherPeople()

    //依然是burger的父类
    consumer1.consume(FastFood())
    consumer2.consume(Burger())
    consumer3.consume(Burger())

}






